package brickdestroyer.simulation;

import brickdestroyer.math.Rectangle2D;
import brickdestroyer.util.ImageResources;
import java.awt.Graphics;
import java.awt.Image;

/**
 * This class represents the slider in the game.
 * <p>It is essentially a rectangle that offers some
 * additional state changes and functionality.
 *
 * @author Momchil Atanasov
 */
public class Slider extends Rectangle2D {

    public static final float DEFAULT_SLIDER_WIDTH = 110.0f;

    public static final float DEFAULT_SLIDER_HEIGHT = 18.0f;

    private static final String SLIDER_IMAGE_ACTIVE_FILE = "pusher_hit.jpg";

    private static final String SLIDER_IMAGE_INACTIVE_FILE = "pusher.jpg";

    private final Image sliderImageActive = ImageResources.getImage(SLIDER_IMAGE_ACTIVE_FILE);

    private final Image sliderImageInactive = ImageResources.getImage(SLIDER_IMAGE_INACTIVE_FILE);

    protected boolean active = false;

    /**
     * Creates a slider object and places it at
     * (0.0f, 0.0f) coordinates.
     * <p>One is required to reposition the slider after
     * construction.
     */
    public Slider() {
        this(0.0f, 0.0f);
    }

    /**
     * Creates a slider object and places it at
     * the specified position.
     * @param x X coordinate of the slider.
     * @param y Y coordinate of the slider.
     */
    public Slider(float x, float y) {
        super(x, y, DEFAULT_SLIDER_WIDTH, DEFAULT_SLIDER_HEIGHT);
    }

    /**
     * This method determines whether the slider will
     * be in active ('hit') or inactive ('released') mode.
     * @param value boolean value representing the active
     * state of the slider.
     */
    public void setActive(boolean value) {
        active = value;
    }

    /**
     * This method checks if the slider is in active mode.
     * @return <code>true</code> if this slider is in active mode,
     * <code>false</code> otherwise.
     */
    public boolean isActive() {
        return active;
    }

    /**
     * This method toggles the slider's state.
     * @see #isActive()
     * @see #setActive(boolean) 
     */
    public void toggle() {
        active = !active;
    }

    /**
     * This method displays the slider on the screen.
     * @param g interface to the graphics handle.
     */
    public void display(Graphics g) {
        Image displayImage = active ? sliderImageActive : sliderImageInactive;
        int sliderX = (int)(x - halfWidth);
        int sliderY = (int)(y - halfHeight);
        int sliderWidth = (int)getWidth();
        int sliderHeight = (int)getHeight();
        g.drawImage(displayImage, sliderX, sliderY, sliderWidth, sliderHeight, null);
    }
}
